angular.module('starter')
.controller("selectHospitalCtrl", function ($scope, $rootScope,appBase, httpService) {
    var navBox = $(".selectHospital .navCondition .navBox");
    var box = $(".selectHospital .navCondition .navBox .box");
    var navLi = $(".selectHospital .navCondition>ul li");
    $scope.navclick = function (idx) {//nav点击下拉选全市区域和智能排序
        navLi.eq(idx).addClass("on").siblings().removeClass("on");
        var thisBox = box.eq(idx);
        thisBox.siblings().hide();
        if (thisBox.is(":hidden")) {
            navBox.show();
            thisBox.show();
            navLi.eq(idx).addClass("on");
        } else {
            navLi.eq(idx).removeClass("on");
            navBox.hide();
            box.hide();
        }
    }
    //当前第几页
    $scope.currentPage = 1;
    //是否还有更多
    $scope.hasMore = true;
    //医院的信息集合
    $scope.hospitalArray = [];

    //点击选全市区域
    $scope.areas = [];
    $scope.types = [
		{ "type_code": "0", "type_name": "智能推荐" },
		{ "type_code": "1", "type_name": "就诊人数排序" },
		{ "type_code": "2", "type_name": "医院等级排序" },
		{ "type_code": "3", "type_name": "服务项目排序" }
    ]
    $scope.areaCon = "全市区域";
    $scope.areaCode = appBase.cityCode;//地区的code
    $scope.areaFn = function ($event) {
        var thisTarget = $event.target;
        $scope.areaCode = thisTarget.attributes[1].value;
        console.log(thisTarget.attributes[1].value)//取得value值
        console.log(thisTarget.innerText);//取得内容

        loadHospital(true);

        $scope.areaCon = thisTarget.innerText;//把取到的选项绑定
        navLi.removeClass("on");
        navBox.hide();
        box.hide();
    }
    $scope.typeCon = "智能排序";
    $scope.sortType = "0";//排序的顺序
    $scope.typeFn = function ($event) {
        var thisTarget = $event.target;
        console.log(thisTarget.attributes[1].value)//取得value值
        console.log(thisTarget.innerText);//取得内容
        $scope.sortType = thisTarget.attributes[1].value;

        loadHospital(true);

        $scope.typeCon = thisTarget.innerText;//把取到的选项绑定
        navLi.removeClass("on");
        navBox.hide();
        box.hide();
    }

    $scope.$watch('$viewContentLoaded', function() {  
        // 你的代码
        httpService.get(httpService.appWebApi.getAreaCode + "?type=区域&code=" + appBase.cityCode)
        .then(
        function(success)
        {
            var data = success.data;
            if (data.returnCode != "0") {
                console.log(data.returnMsg)
                return;
            }
            $scope.areas = [{ "area_code": appBase.cityCode, "area_name": "全市区域" }];
            angular.forEach(data.returnData, function (modal) {
                $scope.areas.push(
                    {
                        "area_code": modal.Sa_Code,
                        "area_name": modal.Sa_Name
                    });
            });

            //加载数据
            loadHospital();
        }
        ,
        function(err)
        {
            console.log(err)
        }
        );
    }); 
 

    $scope.do_infinite = function () {

        if ($scope.hasMore) {
            //$scope.$broadcast("scroll.infiniteScrollComplete");
            console.log("ttttt" + $scope.currentPage);// 
            return;
        }
        //  如果当前页数大于等于总页数，说明已经没数据可再加载了。
        $scope.currentPage += 1;
        console.log($scope.currentPage);// 

    }

    $scope.do_refresher = function () {

        $scope.hospitalArray = [];
        $scope.$broadcast("scroll.refreshComplete");//下拉停止
    }

    //滑到滚动底部加载
    $(".list_box").scroll(function () {
        
        if ($(this).scrollTop() + $(".list_box").height()
            >(parseInt($("#hosplist").height()) - 1)) {
            if (!$scope.hasMore) {
                return;
            }
            $scope.hasMore = false;
            loadHospital();
        }
 
    });

    function loadHospital(isInIt)
    {
        isInIt = arguments[0] || false;
        httpService.parData.clear();
        var parData = httpService.parData;

        if (isInIt) {
            $scope.currentPage = 1;
            $("#selecthospitalTile").html("——&nbsp;&nbsp;向下滑更多 &nbsp;&nbsp;——");
            $scope.hasMore = true;
            $scope.hospitalArray = [];
        }

        parData.cityCode = $scope.areaCode;
        parData.currentPage = $scope.currentPage;
        parData.sortType = $scope.sortType;

        httpService.post(httpService.appWebApi.appGetHospital,
          parData
          ).then(
          function (success) {
              var data = success.data;
              if (data.returnCode != "0") {
                  console.log(data.returnMsg)
                  return;
              }
              $scope.currentPage = $scope.currentPage + 1;
              angular.forEach(data.returnData, function (modal) {
                  $scope.hospitalArray.push(
                      {
                          "name": modal.hsp_Name,
                          "hopId": modal.hsp_Code,
                          "grade": modal.hospitalLevel,
                          "vis": "5855",
                          "imgsrc": modal.pic,
                          "addr":modal.addr,
                          "items": getHospitalItems(modal)

                      });
              });
              if (data.returnData == null || data.returnData.length < 10) {
                  $("#selecthospitalTile").html("——&nbsp;&nbsp;没有更多 &nbsp;&nbsp;——");
                  $scope.hasMore = false;
              } else {
                  $scope.hasMore = true;
              }
          }
          , function (error) {
              console.log(error);
              $scope.hasMore = true;
              $(".list_box").scrollTop(0);
          }).finally(function () {
              $rootScope.$digest();
          });
    }
})